﻿using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class ResBuildWindow : EditorWindow
{
    static ResBuildWindow buildWindow;

    /// <summary>
    /// 预制体的根目录 "Asset/Resources/Prefabs/";
    /// </summary>
    static string prefabPath;


    [MenuItem("工具/资源打包工具", false, 0)]
    static void Init()
    {
        buildWindow = (ResBuildWindow)EditorWindow.GetWindow(typeof(ResBuildWindow), false, "打包工具", true);
        buildWindow.Show();

        prefabPath = "Assets/Resources/Prefabs/";
    }

    private void OnGUI()
    {
        EditorGUILayout.Space();
        if (GUILayout.Button("打印所有的AB包名字"))
        {
            EditorUtility.DisplayProgressBar("打印所有的AB包名字", "打印所有的AB包名字", 0);
            //获取所有设置的AssetBundle 
            string[] names = AssetDatabase.GetAllAssetBundleNames();
            foreach (string name in names)
            {
                Debug.Log("AB名: " + name);
            }
            AssetDatabase.Refresh();
            EditorUtility.ClearProgressBar();
        }
        EditorGUILayout.Space();

        EditorGUILayout.Space();
        if (GUILayout.Button("清除所有AB包的名字"))
        {
            EditorUtility.DisplayProgressBar("清除", "清除所有AB包的名字", 0);
            string[] ABsNames = AssetDatabase.GetAllAssetBundleNames();
            for (int j = 0; j < ABsNames.Length; j++)
            {
                AssetDatabase.RemoveAssetBundleName(ABsNames[j], true);
            }
            //刷新资源
            AssetDatabase.Refresh();
            EditorUtility.ClearProgressBar();
        }
        EditorGUILayout.Space();

        EditorGUILayout.Space();
        if (GUILayout.Button("设置资源的AB名"))
        {
            SetAllAbNames();
        }
        EditorGUILayout.Space();

        EditorGUILayout.Space();
        if (GUILayout.Button("打包所有AB"))
        {
            BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath, BuildAssetBundleOptions.None, BuildTarget.WebGL);
        }
        EditorGUILayout.Space();
    }

    void SetAllAbNames()
    {
        EditorUtility.DisplayProgressBar("设置预制的AB名", "设置预制的AB名", 0);

        string[] pathArr = GetFiles(prefabPath, true);
        for (int j = 0; j < pathArr.Length; j++)
        {
            if (pathArr[j].Contains(".DS_Store") || Path.GetExtension(pathArr[j]) == ".meta")
            {
                continue;
            }
            var progress = (j + 1f) / pathArr.Length;
            EditorUtility.DisplayProgressBar("更新prefab的ABName", pathArr[j], progress);
            Debug.Log(pathArr[j]);
            var importer = AssetImporter.GetAtPath(pathArr[j]);
            if (importer != null)
            {
                Debug.Log(Path.GetFileNameWithoutExtension(pathArr[j]));
                importer.assetBundleName = Path.GetFileNameWithoutExtension(pathArr[j]);
                importer.assetBundleVariant = "t";
            }
        }
        AssetDatabase.Refresh();
        EditorUtility.ClearProgressBar();
    }

    /// <summary>
    /// 获取目录下的所有对象路径，去掉了.meta
    /// </summary>
    /// <param name="path">目录路径</param>
    /// <param name="recursive">是否递归获取</param>
    /// <returns></returns>
    public static string[] GetFiles(string path, bool recursive = true)
    {
        var resultList = new List<string>();
        var dirArr = Directory.GetFiles(path, "*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
        for (int i = 0; i < dirArr.Length; i++)
        {
            if (Path.GetExtension(dirArr[i]) != ".meta")
            {
                resultList.Add(dirArr[i].Replace('\\', '/'));
            }
        }
        return resultList.ToArray();
    }

    public static string FormatPath(string path)
    {
        if (string.IsNullOrEmpty(path))
        {
            return string.Empty;
        }
        path = path.Replace('\\', '.');
        return path.Replace('/', '.');
    }
}